<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/sys-admin' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>添加轮播图</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-form-item label="手机id" prop="phoneIds">
        <el-select v-model="ruleForm.phoneIds" multiple placeholder="请选择">
          <el-option
              v-for="item in phoneListOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="轮播图图片">
        <!--limit限制图片上传的数量-->
        <el-upload
            limit="1"
            action="http://localhost:9081/upload"
            name="picFile"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-success="handleSuccess"
            :on-remove="handleRemove">
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      phoneListOptions:[],
      ruleForm: {
        phoneIds: [],
        url: '',
        dialogImageUrl: '',
        dialogVisible: false,
      },

    };
  },
  methods: {
    loadPhoneList() {
      console.log('准备从服务器端加载【手机列表】……');
      let url = 'http://localhost:9081/phones';
      console.log('请求路径：' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url)
          .then((response) => {
            let responseBody = response.data;
            console.log('接收到服务器端响应的结果：');
            console.log(responseBody);
            this.phoneListOptions = responseBody.data;
          });
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
      //file.response得到的是文件上传成功时服务器响应的内容(此内容就是图片的路径)
      let url = file.response;
      //发出删除图片的请求
      this.axios.get("/remove?url="+url).then(function () {
        alert("服务器中文件删除成功!");
        this.url="";
      })
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleSuccess(response,file,fileList){
      //把服务器响应的图片路径 给到Vue的变量
      this.ruleForm.url = response;
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9081/banners/add-new';
          console.log('尝试添加品牌……');
          console.log('请求路径为：' + url);
          console.log('请求参数为：' + this.ruleForm);
          console.log(this.ruleForm);
          let formData = this.qs.stringify(this.ruleForm);
          console.log('将ruleForm对象转换为FormData：');
          console.log(formData);
          this.axios.post(url, formData).then((response) => {
            console.log('服务器端响应的结果：' + response);
            console.log(response);
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              console.log('添加轮播图成功');
              this.$message({
                message: '添加轮播图成功！',
                type: 'success'
              });
            } else {
              console.log(responseBody.message);
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    console.log('mounted');
    this.loadPhoneList();
  }
}
</script>

<style scoped>

</style>